package com.collect.cameroon.zhiScene.zhiWidget.zhiSendSmsView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import androidx.fragment.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.collect.cameroon.R;
import com.collect.cameroon.zhiBean.zhiSend.ZHISCaseEntity;
import com.collect.cameroon.zhiHelper.ZHISysShareHelper;
import com.collect.cameroon.zhiScene.zhiWidget.zhiStepView.ZHINextStepView;
import com.collect.cameroon.zhiUtils.StringUtil;
import com.collect.cameroon.zhiUtils.ToastUtil;

public class ZHISendRepaymentLinkView extends PopupWindow {

    private View mMenuView;
    private TextView tv_sms_content;
    private TextView tv_name;
    private TextView tv_phone;
    private ZHINextStepView btn_negtive;
    private ZHINextStepView btn_negtive_whatsapp;

    private OnSendRepaymentLinkClickListener mOnSendRepaymentLinkClickListener;
    private Context mContext;
    private FragmentManager mFragmentManager;
    private View mParentView;
    private String mPhone;

    @SuppressLint("ClickableViewAccessibility")
    public ZHISendRepaymentLinkView(Context context) {
        super(context);
        mContext = context;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.popview_send_repayment_link_view, null);
        tv_sms_content = mMenuView.findViewById(R.id.tv_sms_content);
        tv_name = mMenuView.findViewById(R.id.tv_name);
        tv_phone = mMenuView.findViewById(R.id.tv_phone);
        btn_negtive = mMenuView.findViewById(R.id.btn_negtive);
        btn_negtive_whatsapp = mMenuView.findViewById(R.id.btn_negtive_whatsapp);

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.PopWindowFromBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xa0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setClippingEnabled(false);

        btn_negtive.setOnMenuItemDidChangedListener(item -> {
            dismiss();
            if (mOnSendRepaymentLinkClickListener != null) {
                mOnSendRepaymentLinkClickListener.onSendRepaymentLinkClick(tv_sms_content.getText().toString().trim());
            }
        });
        btn_negtive_whatsapp.setOnMenuItemDidChangedListener(item -> {
            String content = tv_sms_content.getText().toString().trim();
            if(TextUtils.isEmpty(content)){
                ToastUtil.s("Repayment Link is empty!");
                return;
            }
            dismiss();
            ZHISysShareHelper.copyToClipboard(mContext, tv_sms_content.getText().toString().trim());
            if (mOnSendRepaymentLinkClickListener != null) {
                mOnSendRepaymentLinkClickListener.onSendRepaymentLinkWithWhatsAppClick(mPhone, content);
            }
        });
        tv_sms_content.setOnLongClickListener(v -> {
            String content = tv_sms_content.getText().toString().trim();
            if(!TextUtils.isEmpty(content)){
                ZHISysShareHelper.copyToClipboard(mContext, content);
            }
            return false;
        });

        mMenuView.setOnTouchListener((v, event) -> {
            int height = mMenuView.findViewById(R.id.ll_pop_layout).getTop();
            int y = (int) event.getY();
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (y < height) {
                    dismiss();
                }
            }
            return true;
        });
    }

    public void setFragmentManager(FragmentManager fragmentManager) {
        mFragmentManager = fragmentManager;
    }

    public void setmParentView(View parentView) {
        mParentView = parentView;
    }

    public void setName(String name) {
        try {
            tv_name.setText(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getName(){
        return StringUtil.safeString(tv_name.getText().toString());
    }

    public void setContact(String contact) {
        try {
            mPhone = StringUtil.safeString(contact);
            String contactEncryptFormatter = StringUtil.phoneNumberEncryptFormatter(contact);
            tv_phone.setText(contactEncryptFormatter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setRepaymentLink(String productName, String repaymentLink) {
        try {
            String smsContent = productName+" Repayment Link: \n"+repaymentLink;
            tv_sms_content.setText(smsContent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void fillRecTagParams(ZHISCaseEntity zhiSCaseEntity) {
        try {


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setOnSendRepaymentLinkClickListener(OnSendRepaymentLinkClickListener sendRepaymentLinkClickListener) {
        this.mOnSendRepaymentLinkClickListener = sendRepaymentLinkClickListener;
    }

    public interface OnSendRepaymentLinkClickListener {
        void onSendRepaymentLinkClick(String content);
        void onSendRepaymentLinkWithWhatsAppClick(String mobile, String content);
    }
}
